package com.fanbai.score.business.fragemnt;

/**
 * Created by acer on 2017/5/6.
 */

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.fanbai.score.R;
import com.fanbai.score.business.activity.ActAddActivity;
import com.fanbai.score.business.activity.ShopList_Activity;
import com.fanbai.score.business.http.Busin_Goods_RequestNet;
import com.fanbai.score.business.http.Busin_RequestNet;
import com.fanbai.score.business.http.Busin_User_RequestNet;
import com.fanbai.score.common.Utils.ImageUtils;
import com.fanbai.score.common.Utils.JsonUtils;
import com.fanbai.score.common.Utils.ToastUtils;
import com.fanbai.score.common.activity.CommonList_Activity;
import com.fanbai.score.common.activity.LoginActivity;
import com.fanbai.score.common.activity.WebView_Activity;
import com.fanbai.score.common.base.BaseFragment;
import com.fanbai.score.common.http.CallBack;
import com.fanbai.score.common.zxing.activity.CaptureActivity;
import com.fanbai.score.user.bean.HeadImage_Bean;
import com.fanbai.score.user.http.UserRequestNetwork_Utils;
import com.jude.rollviewpager.OnItemClickListener;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * A placeholder fragment containing a simple view.
 */
public class Home_Fragment extends BaseFragment {

    @BindView(R.id.Tenant_rollPagerView)
    RollPagerView mRollViewPager;
    @BindView(R.id.businessHomeFragment_totleParce)
    TextView totlePraceTxt;

    List<HeadImage_Bean> headImageBeanList;//轮播图数据

    @OnClick({R.id.businessHomeFragment_lingQuBut, R.id.businessHomeFragment_shopListBut,
            R.id.businessHomeFragment_addActBut, R.id.businessHomeFragment_shopActBut})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.businessHomeFragment_lingQuBut:
                ctx.showDialog();
                Busin_RequestNet.pickuptodaycoin(ctx, new CallBack<String>() {
                    @Override
                    public void onSuccess(String code, String msg, String s) {
                        ctx.hideDialog();
                        ToastUtils.RightImageToast(ctx, "领取了"+JsonUtils.getString(s, "incomecoin")+"贵人币");
                    }

                    @Override
                    public void onFailure(String code, String msg, String body) {
                        ctx.hideDialog();
                        ToastUtils.WarnImageToast(ctx, msg);
                    }

                    @Override
                    public void onError(String code, String msg) {
                        ctx.hideDialog();
                        ToastUtils.ErrorImageToast(ctx, msg);
                    }
                });
                break;
            case R.id.businessHomeFragment_shopListBut:
                ctx.jumpToActivity(ShopList_Activity.class);
                break;
            case R.id.businessHomeFragment_addActBut:
                ToastUtils.RightImageToast(ctx, "建设中");
                break;
            case R.id.businessHomeFragment_shopActBut:
                ToastUtils.RightImageToast(ctx, "建设中");
                break;
        }
    }

    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static Home_Fragment newInstance(int sectionNumber) {
        Home_Fragment fragment = new Home_Fragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public Home_Fragment() {
    }


    @Override
    protected int getLayoutID() {
        return R.layout.business_fragment_home;
    }

    @Override
    public String getTitleName() {
        return "老板";
    }

    @Override
    public void iniTitleLeft(ImageView imageView) {
        imageView.setImageResource(R.mipmap.ic_launcher);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().startActivityForResult(new Intent(ctx, CaptureActivity.class),0);
            }
        });
    }

    @Override
    public void iniTitleRight(TextView tv) {
        tv.setText("退出");
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ctx.jumpToActivity(LoginActivity.class);
            }
        });
    }

    @Override
    protected void initView(View view) {
    }

    @Override
    protected void initData() {
        getHeadImages();
        getTotalPriceForToday();
    }

    @Override
    protected void initListener() {
        mRollViewPager.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (headImageBeanList.size() > position) {
                    HeadImage_Bean bean = headImageBeanList.get(position);
                    if (bean.getUrl() != null && !bean.getUrl().equals("")) {
                        Bundle bundle = new Bundle();
                        bundle.putString("url", "" + bean.getUrl());
                        ctx.jumpToActivity(WebView_Activity.class, bundle);
                    }
                }
            }
        });
    }

    private void getHeadImages(){
        Busin_User_RequestNet.getHeadImages1(ctx, new CallBack<List<HeadImage_Bean>>() {
            @Override
            public void onSuccess(String code, String msg, List<HeadImage_Bean> beans) {
                headImageBeanList = beans;
                //2个Adapter用法一样;
                TestNomalAdapter mAdapter = new TestNomalAdapter(headImageBeanList);
                mRollViewPager.setAdapter(mAdapter);
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                ToastUtils.WarnImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                ToastUtils.ErrorImageToast(ctx, msg);
            }
        });
    }

    private class TestNomalAdapter extends StaticPagerAdapter {
        List<HeadImage_Bean> headImage_been;

        public TestNomalAdapter(List<HeadImage_Bean> headImage_been) {
            this.headImage_been = headImage_been;
        }

        @Override
        public View getView(ViewGroup container, int position) {
            ImageView view = new ImageView(container.getContext());
            ImageUtils.show(ctx, headImage_been.get(position).getImage(),
                    view, R.mipmap.icon_img_defeated);
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            return view;
        }

        @Override
        public int getCount() {
            return headImage_been.size();
        }
    }

    private void getTotalPriceForToday(){
        ctx.showDialog();
        Busin_Goods_RequestNet.gettotalpricefortoday(ctx, new CallBack<String>() {
            @Override
            public void onSuccess(String code, String msg, String s) {
                ctx.hideDialog();
                totlePraceTxt.setText("今日店铺总消费金额"+s+"元");
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                ctx.hideDialog();
            }

            @Override
            public void onError(String code, String msg) {
                ctx.hideDialog();
            }
        });
    }
}
